﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using Newtonsoft.Json;
using System.Data;
using System.IO;

namespace HouseProperty.Controllers
{
    public class HouseController : Controller
    {
        bll bll = new bll();
        #region Index 首頁 權限 左側導航欄

        public ActionResult Index()
        {
            var user = Session["LoginUser"] as SysUser;
            ViewBag.roleId = user.roles_id;
            return View();
        }
        public ActionResult Welcome()
        {
            return View();
        }
        /// <summary>
        ///  绑定根权限
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public string LeftBindingParent(int RoleId)
        {
            var bingpar = bll.LeftBindingParent(RoleId);
            return JsonConvert.SerializeObject(bingpar);
        }
        /// <summary>
        ///  绑定子权限
        /// </summary>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        public string LeftBindingSon(int ParentId)
        {
            var bingson = bll.LeftBindingSon(ParentId);
            return JsonConvert.SerializeObject(bingson);
        }
        #endregion
        #region 項目立項

        /// <summary>
        ///  项目立项 -- 我的立项/列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult CapitalList(int? id)
        {
            var user = Session["LoginUser"] as SysUser;
            if (id == null || id <= 0)
            {
                ViewBag.list = bll.CapitalList(user.users_Id);
            }
            else
            {
                ViewBag.list = bll.GetListByDepartmentID(id);
            }
           
            return View();
        }
        public ActionResult CapitalAdd()
        {
            return View();
        }
        /// <summary>
        ///  项目立项 -- 添加项目
        /// </summary>
        /// <param name="bas"></param>
        /// <returns></returns>
        public ActionResult CapitalAddDo(EABasic_Info bas)
        {
            if (bll.CapitalAdd(bas) > 0)
            {
                return Content("<script>alert('立项成功!');location.href='/House/CapitalList'</script>");
            }
            else
            {
                return Content("<script>alert('立项失败!');location.href='/House/CapitalList'</script>");
            }
        }
        /// <summary>
        ///  项目立项 -- 新建借款人/可批量添加
        /// </summary>
        /// <returns></returns>
        public ActionResult Loan_Info()
        {
            ViewBag.dic = bll.dictionary("房屋用途");
            return View();
        }
        public ActionResult loanAdd(EALoan_Info l)
        {
            var user = Session["loginUser"] as SysUser;
            l.create_People = user.users_Id.ToString();
            if (bll.Loan_Info(l) > 0)
            {
                return Content("<script>alert('添加借款人成功!');location.href='/House/CapitalList'</script>");
            }
            else
            {
                return Content("<script>alert('添加失败!');location.href='/House/CapitalList'</script>");
            }
        }
        #endregion
        #region 項目勘察
        /// <summary>
        ///  项目勘察 -- 我的勘察/列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult exploreList()
        {
            var user = Session["LoginUser"] as SysUser;
            return View(bll.exploreList(user.users_Id));
        }
        /// <summary>
        ///  项目勘察 -- 开始勘察
        /// </summary>
        /// <param name="projectNumber"></param>
        /// <returns></returns>
        public int startExplore(int projectNumber)
        {
            return bll.startExplore(projectNumber);
        }
        /// <summary>
        ///  项目勘察 -- 区位状况勘察
        /// </summary>
        /// <returns></returns>
        public ActionResult LocationAdd(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        ///  项目勘察 -- 区位状况勘察
        /// </summary>
        /// <returns></returns>
        public ActionResult LocationAddDo(EALocation l)
        {
            if (bll.LocationAdd(l) > 0)
            {
                return Content("<script>alert('区位状况已保存!');location.href='/House/exploreList';</script>");
            }
            else
            {
                return Content("<script>alert('区位状况保存失败!');location.href='/House/exploreList';</script>");
            }
        }
        /// <summary>
        ///  项目勘察 -- 小区状况勘察
        /// </summary>
        /// <returns></returns>
        public ActionResult VillageAdd(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        ///  项目勘察 -- 小区状况勘察
        /// </summary>
        /// <returns></returns>
        public ActionResult VillageAddDo(EAVillage v)
        {
            if (bll.VillageAdd(v) > 0)
            {
                return Content("<script>alert('小区状况已保存!');location.href='/House/exploreList';</script>");
            }
            else
            {
                return Content("<script>alert('小区状况保存失败!');location.href='/House/exploreList';</script>");
            }
        }
        /// <summary>
        ///  项目勘察 -- 公共区域状况勘察
        /// </summary>
        /// <returns></returns>
        public ActionResult FloorsAdd(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        ///  项目勘察 -- 公共区域状况勘察
        /// </summary>
        /// <returns></returns>
        public ActionResult FloorsAddDo(Floors f)
        {
            if (bll.FloorsAdd(f) > 0)
            {
                return Content("<script>alert('公共区域状况已保存!');location.href='/House/exploreList';</script>");
            }
            else
            {
                return Content("<script>alert('公共区域状况保存失败!');location.href='/House/exploreList';</script>");
            }
        }
        /// <summary>
        ///  项目勘察 -- 室内状况勘察
        /// </summary>
        /// <returns></returns>
        public ActionResult IndoorAdd(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        ///  项目勘察 -- 室内状况勘察
        /// </summary>
        /// <returns></returns>
        public ActionResult IndoorAddDo(EAIndoor i)
        {
            if (bll.IndoorAdd(i) > 0)
            {
                return Content("<script>alert('室内状况已保存!');location.href='/House/exploreList';</script>");
            }
            else
            {
                return Content("<script>alert('室内状况保存失败!');location.href='/House/exploreList';</script>");
            }
        }
        /// <summary>
        ///  项目勘察 -- 现场照片提供
        /// </summary>
        /// <returns></returns>
        public ActionResult PhotoAdd(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        ///  项目勘察 -- 现场照片提供
        /// </summary>
        /// <returns></returns>
        public ActionResult PhotoAddDo(EAPhoto p)
        {
            if (bll.PhotoAdd(p) > 0)
            {
                return Content("<script>alert('现场照片已保存!');location.href='/House/exploreList';</script>");
            }
            else
            {
                return Content("<script>alert('现场照片保存失败!');location.href='/House/exploreList';</script>");
            }
        }
        //后台代码
        //为了简便这里就不对图片进行压缩等处理了。。。
        [HttpPost]
        public JsonResult UploadImage(HttpPostedFileBase image)
        {
            if (image != null)
            {
                string vPath = TempBasePicUrl + Guid.NewGuid().ToString() + ".png";
                string tempFilePath = Server.MapPath("~" + vPath);
                image.SaveAs(tempFilePath);
                return Json(new { success = true, result = vPath });
            }
            else
            {
                return Json(new { success = false, msg = "图片上传失败，请重新尝试！" });
            }
        }
        private string TempBasePicUrl
        {
            get
            {
                string temp = "/files/";
                string dir = Server.MapPath("~" + temp);
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }
                return temp;
            }
        }

        /// <summary>
        ///  项目勘察 -- 添加询价结果
        /// </summary>
        /// <returns></returns>
        public ActionResult InquiryAdd()
        {
            return View();
        }
        /// <summary>
        ///  项目勘察 -- 添加询价结果
        /// </summary>
        /// <returns></returns>
        public ActionResult InquiryAddDo(EAInquiry q)
        {
            var user = Session["LoginUser"] as SysUser;
            q.inquiry_CreatePeople = user.users_Id.ToString();
            if (bll.InquiryAdd(q) > 0)
            {
                return Content("<script>alert('现场照片已保存!');location.href='/House/exploreList';</script>");
            }
            else
            {
                return Content("<script>alert('现场照片保存失败!');location.href='/House/exploreList';</script>");
            }
        }
        /// <summary>
        ///  项目勘察 -- 询价记录
        /// </summary>
        /// <returns></returns>
        public ActionResult InquiryList(int id)
        {
            return View(bll.InquiryList(id));
        }
        /// <summary>
        ///  项目勘察 -- 勘察详情
        /// </summary>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            return View(bll.Details(id));
        }
        /// <summary>
        ///  项目勘察 --  提交勘察记录
        /// </summary>
        /// <returns></returns>
        public int CommitCapital(int Basic_id)
        {
            return bll.CommitCapital(Basic_id);
        }
        /// <summary>
        ///  项目勘察 -- 项目审核记录
        /// </summary>
        /// <returns></returns>
        public ActionResult exploreAuditingList()
        {
            return View(bll.EAAuditingXQ());
        }
        #endregion
        #region 項目評估

        /// <summary>
        ///  项目评估 -- 我的评估/列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult assessList()
        {
            var user = Session["loginUser"] as SysUser;
            return View(bll.assessList(1));
        }
        /// <summary>
        ///  项目评估 -- 勘察详情
        /// </summary>
        /// <returns></returns>
        public ActionResult assesserList()
        {
            return View();
        }
        /// <summary>
        ///  项目评估 -- 评估详情
        /// </summary>
        /// <returns></returns>
        public ActionResult assessXX()
        {
            return View();
        }
        /// <summary>
        ///  项目评估 -- 开始评估
        /// </summary>
        /// <returns></returns>
        public ActionResult assessAdd(int id)
        {
            return View(bll.Details(id));
        }

        #endregion
        #region 項目審核
        /// <summary>
        ///  项目审核 -- 我的审核列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult AuditingList()
        {
            var user = Session["LoginUser"] as SysUser;
            List<EABasic_Info> list = null;
            if (user.roles_id == 5)
            {
                list = bll.exploreAuditingList(5.ToString());
            }
            if (user.roles_id == 6)
            {
                list = bll.exploreAuditingList(7.ToString());
            }
            if (user.roles_id == 1)
            {
                string str = 5.ToString() + ',' + 7.ToString();
                list = bll.exploreAuditingList(str);
            }
            return View(list);
        }
        #endregion
        #region 系統管理
        /// <summary>
        ///  系统管理 -- 部门管理
        /// </summary>
        /// <returns></returns>
        public ActionResult DeptList()
        {
            return View(bll.deptList());
        }
        /// <summary>
        /// 系统管理 -- 部门管理  -- 添加部门
        /// </summary>
        /// <param name="dept"></param>
        /// <returns></returns>
        public ActionResult DeptAdd()
        {
            return View();
        }
        /// <summary>
        /// 系统管理 -- 部门管理  -- 添加部门
        /// </summary>
        /// <param name="dept"></param>
        /// <returns></returns>
        public ActionResult DeptAddDo(SysDepartment dept)
        {
            if (bll.deptAdd(dept) > 0)
            {
                return Content("<script>alert('添加部门成功！');location.href='/House/DeptList';</script>");
            }
            else
            {
                return Content("<script>alert('添加部门失败！');location.href='/House/DeptList';</script>");
            }
        }
        /// <summary>
        ///  系统管理 -- 角色管理   -- 角色列表
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleList()
        {
            return View(bll.rolesList());
        }
        /// <summary>
        ///  系统管理 -- 角色管理   -- 添加角色试图
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleAdd()
        {
            return View();
        }
        /// <summary>
        ///  系统管理 -- 角色管理   -- 添加角色方法
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleAddDo(SysRoles role)
        {
            return View(bll.rolesAdd(role));
        }
        /// <summary>
        ///  系统管理 -- 用户管理
        /// </summary>
        /// <returns></returns>
        public ActionResult UserList()
        {
            return View(bll.userList());
        }
        public ActionResult UserAdd()
        {
            return View();
        }
        public ActionResult UserAddDo(SysUser u)
        {
            return View(bll.userAdd(u));
        }
        /// <summary>
        ///  系统管理 -- 菜单管理（权限管理）
        /// </summary>
        /// <returns></returns>
        public ActionResult AuthorityList()
        {
            return View();
        }
        /// <summary>
        ///  系统管理 -- 权限设置(修改角色权限)
        /// </summary>
        /// <returns></returns>
        public ActionResult Authority()
        {
            return View();
        }
        /// <summary>
        ///  系统管理 -- 字典管理
        /// </summary>
        /// <returns></returns>
        public ActionResult DictionaryManage()
        {
            return View();
        }
        #endregion
    }
}
